import { Router } from 'express';
import { authenticateClient } from '../../middleware/auth';
import { ClientUserController } from '../../api/controllers/userController';
import { uploadAvatar } from '../../middleware/upload';
import { logOperation } from '../../middleware/operationLog';

const router = Router();
const userController = new ClientUserController();

/**
 * @swagger
 * /api/client/users/profile:
 *   get:
 *     summary: 获取当前用户信息
 *     tags: [客户端用户]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 *       401:
 *         description: 未授权
 */
router.get('/profile', authenticateClient, userController.getProfile);

/**
 * @swagger
 * /api/client/users/update-profile:
 *   put:
 *     summary: 更新个人资料
 *     tags: [客户端用户]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               realName:
 *                 type: string
 *                 description: 真实姓名
 *               email:
 *                 type: string
 *                 format: email
 *                 description: 邮箱
 *               phone:
 *                 type: string
 *                 description: 手机号
 *     responses:
 *       200:
 *         description: 更新成功
 *       400:
 *         description: 请求参数错误
 */
router.put('/update-profile', [
  authenticateClient,
  logOperation('更新个人资料', '用户更新', (req: any) => req.user?.id?.toString() || '')
], userController.updateProfile);

/**
 * @swagger
 * /api/client/users/change-password:
 *   post:
 *     summary: 修改密码
 *     tags: [客户端用户]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - currentPassword
 *               - newPassword
 *             properties:
 *               currentPassword:
 *                 type: string
 *                 description: 当前密码
 *               newPassword:
 *                 type: string
 *                 minLength: 6
 *                 description: 新密码
 *     responses:
 *       200:
 *         description: 修改成功
 *       400:
 *         description: 当前密码错误
 */
router.post('/change-password', [
  authenticateClient,
  logOperation('修改密码', '用户更新', (req: any) => req.user?.id?.toString() || '')
], userController.changePassword);

/**
 * @swagger
 * /api/client/users/upload-avatar:
 *   post:
 *     summary: 上传头像
 *     tags: [客户端用户]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         multipart/form-data:
 *           schema:
 *             type: object
 *             required:
 *               - avatar
 *             properties:
 *               avatar:
 *                 type: string
 *                 format: binary
 *                 description: 头像文件
 *     responses:
 *       200:
 *         description: 上传成功
 *       400:
 *         description: 文件格式错误
 */
router.post('/upload-avatar', [
  authenticateClient,
  uploadAvatar,
  logOperation('上传头像', '用户更新', (req: any) => req.user?.id?.toString() || '')
], userController.uploadAvatar);

/**
 * @swagger
 * /api/client/users/badges:
 *   get:
 *     summary: 获取用户徽章
 *     tags: [客户端用户]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 message:
 *                   type: string
 *                   example: 获取用户徽章成功
 *                 data:
 *                   type: array
 *                   items:
 *                     type: object
 *                     properties:
 *                       id:
 *                         type: integer
 *                       earnedAt:
 *                         type: string
 *                         format: date-time
 *                       badge:
 *                         $ref: '#/components/schemas/Badge'
 */
router.get('/badges', authenticateClient, userController.getUserBadges);

/**
 * @swagger
 * /api/client/users/stats:
 *   get:
 *     summary: 获取用户统计信息
 *     tags: [客户端用户]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 message:
 *                   type: string
 *                   example: 获取用户统计信息成功
 *                 data:
 *                   type: object
 *                   properties:
 *                     badgeCount:
 *                       type: integer
 *                       description: 徽章数量
 *                     voteCount:
 *                       type: integer
 *                       description: 投票次数
 *                     performanceCount:
 *                       type: integer
 *                       description: 参与的演出数量
 *                     totalPoints:
 *                       type: integer
 *                       description: 总积分
 */
router.get('/stats', authenticateClient, userController.getUserStats);

export default router; 
